package com.example.dbcms.controller.user;

import com.example.dbcms.annotation.Log;
import com.example.dbcms.domain.Residents;
import com.example.dbcms.domain.User;
import com.example.dbcms.service.PropertyFeeService;
import com.example.dbcms.service.ResidentService;
import com.example.dbcms.service.UserService;
import com.example.dbcms.utils.ResultVoUtil;
import com.example.dbcms.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;



@Controller
@RequestMapping("/user")
@Api("普通用户基本信息路由路由")
public class UserResidentController {

    /**
     * 注入userService
     */
    @Autowired
    private UserService userService;

    @Autowired
    private ResidentService residentService;


    /**
     * 普通用户首页
     * @return
     */
    @GetMapping("/index")
    @ApiOperation("普通用户首页")
    @Log(type="普通用户首页")
    public String index(ModelMap modelMap){
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        User user = userService.getUserByUserAccount(userAccount);
        Residents resident = residentService.getResidentByUserId(user.getUserId());
        modelMap.put("resident", resident);
        return "user/resident/index";
    }

    /**
     * 通过id查找居民信息跳转到编辑页面
     * @param id
     * @param modelMap
     * @return
     */
    @ApiOperation("弹出编辑居民信息页面")
    @GetMapping("/edit/{id}")
    @Log(type="弹出编辑居民信息页面")
    public String editResidentInfo(@PathVariable("id") int id, ModelMap modelMap){
        modelMap.put("resident",residentService.getResidentById(id));
        return "user/resident/edit";
    }

    /**
     * 修改居民信息
     * @param residents 居民信息
     * @return
     */
    @ApiOperation("修改居民用户信息")
    @PostMapping("/updateResident")
    @ResponseBody
    @Log(type="修改居民用户信息")
    public ResultVo saveResident(Residents residents){
        if(residentService.updateUserByUser(residents)==1){
            return ResultVoUtil.SAVE_SUCCESS;
        }
        return ResultVoUtil.error("更新失败");
    }


}
